<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="物料计划名称" prop="mtrProName">
        <el-input v-model="queryParams.mtrProName" placeholder="请输入物料计划名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['mes:mtrplan:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['mes:mtrplan:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['mes:mtrplan:remove']">删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['mes:mtrplan:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="mtrplanList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="序号" align="center" prop="id" /> -->
      <el-table-column label="物料计划编码" align="center" prop="code" />
      <el-table-column label="销售订单名称" align="center" prop="proOrder.orderName" />
      <el-table-column label="销售订单编号" align="center" prop="proOrder.orderNumber" />
      <el-table-column label="产品名称" align="center" prop="proInfo.proName" />
      <el-table-column label="产品编码" align="center" prop="proInfo.proCode" />
      <el-table-column label="订单需求数量" align="center" prop="proOrder.orderDemandNum" />
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column label="计划状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag :type="tagType(scope.row.status)" effect="dark">
            <dict-tag :options="dict.type.mes_mtrplan_status" :value="scope.row.status" />
          </el-tag>

        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['mes:mtrplan:edit']">修改</el-button> -->
          <el-button v-if="scope.row.status != 1" size="mini" type="text" icon="el-icon-edit" @click="xiada(scope.row)"
            v-hasPermi="['mes:mtrplan:edit']">下达</el-button>
          <el-button v-else size="mini" type="text" icon="el-icon-delete" @click="revokePlan(scope.row)"
            v-hasPermi="['mes:mtrplan:remove']">撤回</el-button>
            <el-button  size="mini" type="text" icon="el-icon-delete" @click="getDetails(scope.row)"
            v-hasPermi="['mes:mtrplan:remove']">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改物料计划对话框 -->
    <el-dialog title="物料清单" :visible.sync="open" width="500px" append-to-body>
      <el-table :data="mtrlist" stripe style="width: 100%">
        <el-table-column prop="mtrName" label="原料名称" align="center">
        </el-table-column>
        <el-table-column prop="mtrCode" label="原料编码" align="center">
        </el-table-column>
        <el-table-column prop="mtrQuantity" label="数量" align="center">
        </el-table-column>
        <el-table-column prop="remark" label="备注" align="center">
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>



     <!-- 物料计划详情对话框 -->
     <el-dialog title="物料详情" :visible.sync="detailOpen" width="500px" append-to-body v-if="detailOpen">

      <label style="margin-bottom: 5px;">
          物料计划编码：{{MtrPlanVo.code}}
      </label>
      <el-divider></el-divider>
      <el-table :data="MtrPlanVo.mtrs" stripe style="width: 100%">
        <el-table-column prop="mtrName" label="原料名称" align="center">
        </el-table-column>
        <el-table-column prop="mtrCode" label="原料编码" align="center">
        </el-table-column>
        <el-table-column prop="mtrQuantity" label="数量" align="center">
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="detailOpen = false">确 定</el-button>
        <el-button @click="detailOpen = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMtrplan, getMtrplan, delMtrplan, addMtrplan, updateMtrplan, getDetails, createBuyNeed, revokePlanAndNeed } from "@/api/mes/mtrplan";

export default {
  name: "Mtrplan",
  dicts: ['mes_mtrplan_status'],
  data() {
    return {
      //物料计划详情数据
      MtrPlanVo: [],
      //物料计划详情对话框
      detailOpen:false,
      nowid: "",
      //物料清单
      mtrlist: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料计划表格数据
      mtrplanList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        mtrProName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
 /** 查询物料计划详情 */
 getDetails(row){
      getDetails({id: row.id}).then(res =>{
        this.MtrPlanVo = res.data
        this.detailOpen = true
      })
    },

    //标签类型判断
    tagType(status) {
      switch (status) {
        case 0:
          return ""
        case 1:
          return "success"
      }
    },
    //撤回
    revokePlan(row) {
      revokePlanAndNeed({ id: row.id }).then(res => {
        this.$notify({
          title: '成功',
          message: '撤回成功！',
          type: 'success',
          position: 'top-left'
        });
        this.getList()
      })
    },
    //生成采购需求
    xiada(row) {
      this.nowid = row.id
      getDetails({ id: row.id }).then((res) => {
        console.log(res)
        this.mtrlist = res.data.mtrs
        this.open = true
      })

    },
    /** 查询物料计划列表 */
    getList() {
      this.loading = true;
      listMtrplan(this.queryParams).then(response => {
        this.mtrplanList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        progId: null,
        mtrId: null,
        mtrProName: null,
        mtrName: null,
        mtrQuantity: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物料计划";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMtrplan(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改物料计划";
      });
    },
    /** 下达确定按钮 */
    submitForm() {
      createBuyNeed({ id: this.nowid }).then(() => {
        this.open = false;
        this.getList();
        this.$modal.msgSuccess("下达成功");
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除物料计划编号为"' + ids + '"的数据项？').then(function () {
        return delMtrplan(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mes/mtrplan/export', {
        ...this.queryParams
      }, `mtrplan_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
